#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include <armadillo>
#include <iomanip>

using namespace std;
using namespace arma;

#include "clusteringI_def.hpp"
#include "clusteringI_impl.hpp"

void riemmannian_points();
void cal_Kernel_matrix();



/*Dec 10-2013
 * clusteringI_def and clusteringI_impl are the orginals clustering files 
 * to be used with DCT features. As I am trying now a new code using optical 
 * flow features (See Andres' paper), I'm moving to a new project. As for now, 
 * this is not a Riemmann  Technique, only the mean per regions will be used 
 * to cluster similar segments.\
 * 
 * Next project will be using Temporal and spatial information
 */

int
main(int argc, char** argv)
{
  
  // To calculate Riemmannian Points and save in HD:
  //riemmannian_points();
  //To calculate Kernels using the points obtained with previous method.
  //cal_Kernel_matrix();
  
  //Run kkmeans
  std::string path;
  path = "/home/johanna/codes-svn/my_UCSD/"; 
  
  
  field<std::string> vi_names;
  vi_names.load("video_names.txt");
  //vi_names.print("vi_names");
  int summ_percentage = 10;
  
  field<std::string> frames_list;
  
  
  for (uword i = 0 ; i < 1; ++i)// vi_names.n_rows
  {
    
    std::stringstream load_kernel;
    load_kernel << "./Kernel_matrix/Ker_"<< vi_names(i,0) <<".dat";
    
    std::stringstream result;
    result <<"./Results/Perce="<< summ_percentage << "_" << vi_names(i,0) << ".dat";     
    
    std::stringstream GT;
    GT <<"./GT/new_Gp_annotations_" << vi_names(i,0) << ".dat";     // Ground Truth per Grassmann Point
    cout << "GT= " << GT.str() << endl;
    
    std::stringstream video_path;
    video_path << path << vi_names(i,0);
    
    std::stringstream frames_name;
    frames_name << video_path.str() << "/list.txt";
    cout << "frames_name "<< frames_name.str() << endl;
    frames_list.load(frames_name.str()); // se debe cargar por carpeta, tienen diferente # de frames.
    
    
    
    clustering_RM( load_kernel.str(), result.str(), GT.str(), summ_percentage, video_path.str(), frames_list );
  }
  
  
  return 0;
}





/*
 *  int summ_percentage = 20;
 *  for (uword i = 0 ; i < 1; ++i)// vi_names.n_rows
 *  {
 *    
 *    std::stringstream point_name;
 *    point_name << "./grass_points/Gp_" << vi_names(i,0) <<".dat";
 *    
 *    std::string name_Gpoint = point_name .str();
 *    
 *    cout << "name_Gpoint: " << name_Gpoint << endl;
 *    clustering_grass grass(name_Gpoint);
 *    
 *    std::stringstream Ker_name;
 *    Ker_name <<"./Kernel_matrix/Ker_" << vi_names(i,0) << ".dat";     
 *    cout << "To load from "<<  Ker_name.str()<< endl;
 *    
 *    std::stringstream resul;
 *    resul <<"./Results/Perce="<< summ_percentage << "_" << vi_names(i,0) << ".dat";     
 *    
 *    std::stringstream GT;
 *    GT <<"./GT/new_Gp_annotations_" << vi_names(i,0) << ".dat";     // Ground Truth per Grassmann Point
 *    cout << "GT= " << GT.str() << endl;
 *   
 *    //getchar();
 *    grass.euc_cluster(Ker_name.str(), resul.str(),GT.str(), summ_percentage);
 *    //grass.ker_cluster(Ker_name.str(), resul.str(),GT.str(), sum_percentage);
 *    cout << "************************" << endl;
 *    
 * }*/

void
cal_Kernel_matrix()
{
  //std::string path;
  //path = "/home/johanna/codes-svn/my_UCSD/"; // before videos_list 
  
  field<std::string> vi_names;
  vi_names.load("video_names.txt");
  vi_names.print("vi_names");
  
  
  std::string folder_r_pts; // Folder where Riemmannian Points are saved and loaded.
  
  folder_r_pts = "./riemann_points/";
  field<mat> riemann_points;
  int dim = 15; // 15 DCT features
  
  for (uword i = 0 ; i < vi_names.n_rows; ++i)// vi_names.n_rows
  {
    cout << "i= " << i << endl;
    
    
    
    std::stringstream toload_r_pts;
    toload_r_pts << folder_r_pts << vi_names(i,0) <<".dat";
    riemann_points.load(toload_r_pts.str());
    //riemann_points.print();
    std::stringstream save_kernel;
    save_kernel << "./Kernel_matrix/Ker_"<< vi_names(i,0) <<".dat";
    
    Riemann_Kernels r_Ker(riemann_points, dim);
    //r_Ker.Stein(save_kernel.str());
    r_Ker.Stein_faster(save_kernel.str());
  }
}

void
riemmannian_points()
{
  std::string path;
  path = "/home/johanna/codes-svn/my_UCSD/"; 
  
  field<std::string> vi_names;
  vi_names.load("video_names.txt");
  vi_names.print("vi_names");
  
  
  field<std::string> frames_list;
  
  uword ro = 84;
  uword co = 48;
  std::string folder_r_pts; // Folder where Riemmannian Points are saved and loaded.
  folder_r_pts = "./riemann_points/";
  //maximum p is the dimension of the original data. In this case = 60*38 = 2280
  
  
  for (uword i = 0 ; i < vi_names.n_rows; ++i)// vi_names.n_rows
  {
    cout << "i= " << i << endl;
    
    std::stringstream one_video;
    one_video << path <<  vi_names(i,0) <<"/";
    
    std::string video = one_video.str();
    
    
    std::stringstream frames_name;
    frames_name << video << "list.txt";
    cout << "frames_name "<< frames_name.str() ;
    frames_list.load(frames_name.str()); // se debe cargar por carpeta, tienen diferente # de frames.
    
    std::stringstream tosave_r_pts;
    tosave_r_pts << folder_r_pts << vi_names(i,0) <<".dat";
    
    cout << "Riemannian points will be save at "<< tosave_r_pts.str() << endl;
    
    
    clustering_RM c_RM( video, frames_list, ro, co, tosave_r_pts.str() );
  }
  
}



//cmake
/*
 cmake_minimum_required(VERSION 2.8)
project( grassmann_clustering)
find_package( OpenCV REQUIRED)
find_package( Armadillo REQUIRED)
add_executable( run2.exe main_Riemmann_clustering.cpp )
target_link_libraries( run2.exe ${OpenCV_LIBS} -O1 -larmadillo)
  
  
  
 * */
